/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.sbbs.genericdao.dao.jpa;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.transaction.HeuristicMixedException;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.NotSupportedException;
import javax.transaction.RollbackException;
import javax.transaction.SystemException;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
import static org.jboss.shrinkwrap.api.ShrinkWrap.create;
import static org.jboss.shrinkwrap.api.asset.EmptyAsset.INSTANCE;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.sbbs.EmbeddedBaseTest;
import org.sbbs.genericdao.model.Address;
import org.sbbs.genericdao.model.Home;

/**
 *
 * @author Mr.fdc <fdc_messenger@126.com>
 */
@RunWith(Arquillian.class)
public class BaseDaoTestWithOutEjb extends EmbeddedBaseTest {

    @Deployment
    public static Archive<?> createDeployment() {
        JavaArchive jar = create(JavaArchive.class)
                //                .addPackage(DemoEntityDaoEjb.class.getPackage())
                .addAsManifestResource("META-INF/persistence.xml", "persistence.xml")
                .addAsManifestResource(INSTANCE, "beans.xml");

        return jar;
    }

    @Test
    public void persistTest() {
        try {
            this.cleanTestDataModel();
            utx.begin();
            this.getEm().joinTransaction();
            Address a = new Address();
            a.setCity("city");
            a.setState("state");
            a.setStreet("street");
            a.setZip("zip");
            this.getEm().persist(a);
            utx.commit();
            utx.begin();
            this.getEm().joinTransaction();
            Home h = new Home();
            h.setAddress(a);
            h.setType("type");
            this.getEm().persist(h);
            utx.commit();
        } catch (NotSupportedException ex) {
            Logger.getLogger(BaseDaoTestWithOutEjb.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SystemException ex) {
            Logger.getLogger(BaseDaoTestWithOutEjb.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RollbackException ex) {
            Logger.getLogger(BaseDaoTestWithOutEjb.class.getName()).log(Level.SEVERE, null, ex);
        } catch (HeuristicMixedException ex) {
            Logger.getLogger(BaseDaoTestWithOutEjb.class.getName()).log(Level.SEVERE, null, ex);
        } catch (HeuristicRollbackException ex) {
            Logger.getLogger(BaseDaoTestWithOutEjb.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SecurityException ex) {
            Logger.getLogger(BaseDaoTestWithOutEjb.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalStateException ex) {
            Logger.getLogger(BaseDaoTestWithOutEjb.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}
